import { ref, onMounted,shallowRef} from 'vue';
import { getCompanyList } from "@/apis/organizationsManaging/unitManaging"
export function useOrganizations() {
    const companyTreeData = ref([]);
    onMounted(() => {
       getCompanyTreeData();
    })
    const getCompanyTreeData = () => {
        getCompanyList().then(res => {
            if (res.code === 1) {
                console.log("===");
                companyTreeData.value = generateData(res.data.compays);
            } else {
                message.error(res.msg);
            }
        })
    };
    const generateData = (options) => {
        let list = []
        for (let i = 0; i < options.length; i++) {
            const children = [];
            let obj = {
                title: options[i].name,
                id: options[i].id,
                pid: options[i].pid,
                key: options[i].name + options[i].id,
            };
            if (options[i].departs && options[i].departs.length > 0) {
                (obj.children = generateData(options[i].departs))
            }
            list.push(obj)
        }
        return list;
    
    };
    return {
        companyTreeData
    }
}